package com.spring.demo.springdemo.config;

import com.spring.demo.springdemo.annotation.MyMapperScan;
import com.spring.demo.springdemo.annotation.conditional.config.DefinitionalConfig;
import com.spring.demo.springdemo.annotation.conditional.config.RenpuLog;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.*;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author RenPu
 * @Date 2022/4/8 9:15
 * @Version 1.0
 * @Description:  mybatis集成spring的写法
 **/

@ComponentScan("com.spring.demo.springdemo")
@MyMapperScan(basePackage = "com.spring.demo.springdemo.mapper")
@Import(value = {MybatisImportRegisterBeanDefinitio.class})
@EnableAspectJAutoProxy
//@Configuration
public class MybatisMainConfig {


//    /**
//     * 默认装配的实例的名称为方法名称：如下为person,从容器获取也需要根据person进行获取
//     * 当方法名称和从容器获取的实例名称不一致时，可以通过@Bean(name ="")，name属性指定别名进行获取容器实例
//     *
//     * @return
//     */
//    @Bean(name ="person2")
//    public Person person(){
//        return new Person();
//    }
//

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws IOException {

        InputStream asStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(asStream);
        return sqlSessionFactory;
    }


    @Bean
    @ConditionalOnClass(value = {DefinitionalConfig.class})
    public RenpuLog renpuLog(){
        return new RenpuLog();
    }





}
